import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import vue from 'rollup-plugin-vue'
import typescript from 'rollup-plugin-typescript2'
import { defineConfig } from 'rollup'

const formats = ['esm', 'umd', 'cjs']

const output = formats.map(format => ({
  format,
  file: `dist/index.${format}.js`,
  name: format === 'umd' ? 'VTeamUI2Element' : undefined,
  globals: {
    vue: 'Vue',
    'element-ui': 'ELEMENT'
  }
}))

export default defineConfig({
  input: 'components/lib/index.js',
  external: ['vue', 'element-ui', 'node:crypto'],
  output,
  plugins: [
    resolve({
      browser: true,
      preferBuiltins: false,
      extensions: ['.js', '.vue']
    }),
    commonjs(),
    vue({
      css: false,
      style: {
        preprocessOptions: {
          scss: {
            includePaths: ['node_modules']
          }
        }
      },
      template: {
        optimizeSSR: false,
        isProduction: true
      }
    }),
    typescript({
      check: false,
      tsconfigOverride: { 
        include: ['components/**/*'],
        exclude: ['node_modules', 'dist'],
        compilerOptions: {
          declaration: true,
          declarationDir: 'dist/types',
          target: 'es5',
          module: 'esnext',
          allowJs: true,
          moduleResolution: 'node',
          lib: ['esnext', 'dom'],
          sourceMap: true
        }
      },
      abortOnError: false
    })
  ]
})
